#include "03_header.h"

//判断文件/目录是否存在，返回0则不存在，返回1则存在
int is_exist_data(MYSQL *db, char *filename, char *cur_user_pwd,char *username){
    char query[1024];
    char new_path[512];
    snprintf(new_path, sizeof(new_path), "%s%s/", cur_user_pwd, filename);
    printf("new_path = %s\n", new_path);
    sprintf(query,"select count(*) from file_info where username = '%s' and filename = '%s' and path = '%s';", username,filename,new_path);
    mysql_query(db,query);
//    printf("判断的sql：%s\n",query);
//    printf("进入判断\n");
    MYSQL_RES * Res2 = mysql_use_result(db);                                                                                                                                                                                                                                    
    MYSQL_ROW Row2 = mysql_fetch_row(Res2);
    if(atoi(Row2[0]) == 0){
//        printf("当前目录 %s 不存在文件：%s\n",cur_user_pwd,filename);
        mysql_free_result(Res2);
        return 0;
    }
    else{
        mysql_free_result(Res2);
//        printf("当前目录 %s 存在文件：%s\n",cur_user_pwd,filename);
        return 1;
    }

}

//执行逻辑删除
void remove_dir_or_file(MYSQL *db, char *filename,char *cur_user_pwd ,int cur_dirid, char *username){
    char Data[256];
    memcpy(Data, filename, strlen(filename));
    char query[1024];
    printf("filename = %s.\n", filename);
    printf("pwd = %d.\n",cur_dirid);
    printf("tmpPath = %s.\n", cur_user_pwd);

    int ret = is_exist_data(db,Data,cur_user_pwd,username);
    if(ret == 0){
        printf("删除失败，这个目录或者文件不存在\n");
    }
    else{
        sprintf(query,"DELETE FROM file_info WHERE filename = '%s' AND parent_id = %d;", Data, cur_dirid);
        mysql_query(db,query);
        printf("执行了rmdir函数，当前目录id:%d, 执行sql语句：%s \n",cur_dirid,query);
    }
}
